import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件索赔单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件索赔单详情'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出备件索赔单'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备件索赔单'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改备件索赔单'
});
const submitDataDesc = formatMessage({
    id: 'api.message.submitDataDesc',
    defaultMessage: '提交备件索赔单'
});
const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废备件索赔单'
});
const passDataDesc = formatMessage({
    id: 'api.message.passDataDesc',
    defaultMessage: '索赔单审核通过'
});
const rejectDataDesc = formatMessage({
    id: 'api.message.rejectDataDesc',
    defaultMessage: '索赔单驳回'
});
const refuseDataDesc = formatMessage({
    id: 'api.message.refuseDataDesc',
    defaultMessage: '索赔单拒赔'
});
const getPartInvoiceDesc = formatMessage({
    id: 'api.message.getPartInvoiceDesc',
    defaultMessage: '查询备件箱单发票'
});
const partInvoiceDetailsQueryDesc = formatMessage({
    id: 'api.message.partInvoiceDetailsQueryDesc',
    defaultMessage: '查询备件箱单发票清单'
});
const getListHisDesc = formatMessage({
    id: 'api.message.getListHisDesc',
    defaultMessage: '查询备件历史索赔单'
});
const getDetailHisDesc = formatMessage({
    id: 'api.message.getDetailHisDesc',
    defaultMessage: '查询备件历史索赔单详情'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/partClaim/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partClaims/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partClaims/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/partClaims`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/partClaims/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//提交
export const submitData = (id, data) => request(`${baseUrl}/partClaims/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDataDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/partClaims/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));
//审核通过
export const passData = (id, data) => request(`${baseUrl}/partClaims/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDataDesc));
//驳回
export const rejectData = (id, data) => request(`${baseUrl}/partClaims/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDataDesc));
//拒赔
export const refuseData = (id, data) => request(`${baseUrl}/partClaims/${id}/refuse`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, refuseDataDesc));
// 导出
export const exportPartIns = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partClaims/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
// 查询备件箱单发票
export const partInvoicesQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partInvoices/forPartClaim?${queryString}`, null, dto.PartInvoice)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartInvoiceDesc));
};
// 查询备件箱单发票清单
export const partInvoiceDetailsQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partInvoices/forPartClaimDetail?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partInvoiceDetailsQueryDesc));
};
// 查询历史订单列表
export const getListHis = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partClaimHises/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListHisDesc));
};
// 查询历史详情
export const getDetailHis = id =>
    request(`${baseUrl}/partClaimHises/${id}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailHisDesc));

//历史附件下载
export const getHistoryDownload = data => {
    const queryString = stringify(data);
    return request(`${baseUrl}/files/ftpIsDownload?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, null));
};
